# 以centOS 7为基础
FROM ubuntu

# Fix timezone issue
ENV TZ=Asia/ShangHai
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone

WORKDIR /root

RUN apt-get update

# 安装JDK8并设置JAVAHOME
RUN apt-get install -y openjdk-8-jdk
ENV JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-amd64

# 安装openssh-server
RUN apt-get install -y ssh
# ssh without key
RUN ssh-keygen -t rsa -f ~/.ssh/id_rsa -P '' && \
		cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys && \
		chmod 0600 ~/.ssh/authorized_keys

# 安装Hadoop
COPY hadoop-3.1.4.tar.gz .
RUN tar -xzvf hadoop-3.1.4.tar.gz && \
		mv hadoop-3.1.4 /usr/local/hadoop && \
		rm hadoop-3.1.4.tar.gz
ENV HADOOP_HOME=/usr/local/hadoop
ENV PATH=$PATH:/usr/local/hadoop/bin:/usr/local/hadoop/sbin

# 创建相应的目录
RUN mkdir -p ~/hdfs/namenode && \
		mkdir -p ~/hdfs/datanode && \
		mkdir $HADOOP_HOME/logs

# 复制配置文件
COPY config/* /tmp/
RUN mv /tmp/hadoop-env.sh /usr/local/hadoop/etc/hadoop/hadoop-env.sh && \
		mv /tmp/hdfs-site.xml $HADOOP_HOME/etc/hadoop/hdfs-site.xml && \
		mv /tmp/core-site.xml $HADOOP_HOME/etc/hadoop/core-site.xml && \
		mv /tmp/mapred-site.xml $HADOOP_HOME/etc/hadoop/mapred-site.xml && \
		mv /tmp/yarn-site.xml $HADOOP_HOME/etc/hadoop/yarn-site.xml && \
		mv /tmp/slaves $HADOOP_HOME/etc/hadoop/slaves && \
		mv /tmp/start-hadoop.sh ~/start-hadoop.sh && \
		mv /tmp/run-wordcount.sh ~/run-wordcount.sh

# 修改权限
RUN chmod +x ~/start-hadoop.sh && \
    chmod +x ~/run-wordcount.sh && \
    chmod +x $HADOOP_HOME/sbin/start-dfs.sh && \
    chmod +x $HADOOP_HOME/sbin/start-yarn.sh

# format namenode
RUN /usr/local/hadoop/bin/hdfs namenode -format

CMD [ "sh", "-c", "service ssh start;/bin/bash"]
